package product.hongyasms;

import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HongYaSms {

	public static String sendSms(String httpId, String pwd, String cpid,
			String channelid, String tele, String msg) throws HttpException,
			IOException, IllegalArgumentException, NoSuchAlgorithmException {
		HttpClient httpclient = new DefaultHttpClient();
		String param = httpId
				+ "?cpid="
				+ cpid
				+ "&password="
				+ MD5_32bit2(pwd + "_"
						+ parmToString(System.currentTimeMillis() / 1000L)
						+ "_topsky") + "&timestamp="
				+ parmToString(System.currentTimeMillis() / 1000L)
				+ "&channelid=" + channelid + "&msg="
				+ URLEncoder.encode(msg, "utf-8") + "&tele=" + tele;
		HttpPost httppost = new HttpPost(param);
		HttpResponse response = null;
		try {
			response = httpclient.execute(httppost);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 打印服务器返回的状态

		System.out.println(response.getStatusLine());
		HttpEntity Entity = response.getEntity();
		// 打印结果页面

		try {
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if (Entity != null) {
			System.out.println("The Response length is :"
					+ Entity.getContentLength());
		}
		return EntityUtils.toString(Entity).substring(0, 7);
	}

	public static String parmToString(Long l) {
		System.out.println(l.toString());
		return l.toString();
	}

	public static String MD5_32bit2(String readyEncryptStr)
			throws NoSuchAlgorithmException {
		if (readyEncryptStr != null) {
			int pwdlen = 0;
			pwdlen = readyEncryptStr.length();
			readyEncryptStr = readyEncryptStr.substring(0, pwdlen - 8)
					.toUpperCase()
					+ readyEncryptStr.substring(pwdlen - 8, pwdlen);
			StringBuilder su = new StringBuilder();
			MessageDigest md = MessageDigest.getInstance("MD5");

			System.out.println(readyEncryptStr);
			md.update(readyEncryptStr.getBytes());
			byte[] b = md.digest();
			int temp = 0;
			for (int offset = 0, bLen = b.length; offset < bLen; offset++) {
				temp = b[offset];
				if (temp < 0) {
					temp += 256;
				}
				if (temp < 16) {
					su.append("0");
				}
				su.append(Integer.toHexString(temp));
			}
			return su.toString();
		} else {
			return null;
		}
	}

	public static void main(String[] args) throws HttpException, IOException,
			IllegalArgumentException, NoSuchAlgorithmException {
		String str = null;
		str = 
		sendSms("http://admin.sms9.net/houtai/sms.php",
				"MY740520",
				"5380",
				"3527",
				"18192592986",
				"尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合尊敬的用户，本系统更新时间为10月12至10月15日，感谢您的配合");
		System.out.println(str);
	}

}
